var content = document.querySelector('.content');
var container = document.querySelector('.container');
var backBtn = document.getElementById('backBtn');
var searchBtn = document.getElementById('searchBtn');
var tableBody = document.querySelector('.grade-table tbody');
var currentIndex = 1;
var data = []
var backtest = [];
//data = [
//  { Stu_id: 1, Stu_name: '小明', Cou_name: '数学', Stu_major: '计算机科学', Stu_class: '1班', Score: 85 },
//  { Stu_id: 2, Stu_name: '小红', Cou_name: '英语', Stu_major: '外语', Stu_class: '2班', Score: 90 },
//  { Stu_id: 3, Stu_name: '小刚', Cou_name: '物理', Stu_major: '物理学', Stu_class: '3班', Score: 78 }
//];
var tbody = document.querySelector(".grade-table tbody");

$(document).keydown(function(event) {
  if (event.keyCode == 13) { // 13代表回车键的键码
    $('#searchBtn').click();
  }
});

document.addEventListener("DOMContentLoaded", function() {

    $.ajax({
    url: '/management/teaservlet/teaSearchCourseStudents',
    method: 'POST',
    success: function(response) {
        data = response.data;
        console.log(response);
          data.forEach(function(item) {
            var row = document.createElement("tr");
            row.innerHTML = `
              <td>${currentIndex}</td>
              <td>${item.Stu_id}</td>
              <td>${item.Stu_name}</td>
              <td>${item.Cou_name}</td>
              <td>${item.Stu_major}</td>
              <td>${item.Stu_class}</td>
              <td>${item.Score}</td>
              <td><button id="jjw${currentIndex}">退课</button></td>
            `;
            tbody.appendChild(row);
            currentIndex++;
          });

        for (let i=1; i < currentIndex; i++) {
              var jjw = document.getElementById('jjw'+i);
            //  console.log(jjw);
              jjw.addEventListener('click', function() {
                // 编写跳转到修改信息页面的操作
                // window.location.href = "editstupwd.html";
                  var confirmLogout = confirm("确定要为该同学退课吗？");
                    if (confirmLogout) {
                      // 用户点击了确认按钮
                      // 编写退出登录的操作
                      //没选过这个课
                      $.ajax({
                            url: '/management/teaservlet/dropCourse',
                            method: 'POST',
                            data: {
                              count: i
                            },
                            success: function(response) {
                              console.log(response);
                              console.log(response.data);
//                              alert("选课成功！"+i);
                              alert("退课成功！");
                            }
                          });
                    } else {
                      // 用户点击了取消按钮
                      // 可以选择执行其他操作或不进行任何操作
                    }
              });
            }
        }
    });
  var links = document.querySelectorAll('.sidebar ul li a');

    for (var i = 0; i < links.length; i++) {
      links[i].addEventListener('mouseenter', function() {
        this.style.transition = '0.2s';
        this.style.backgroundColor = '#679';
        this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
      });

      links[i].addEventListener('mouseleave', function() {
        this.style.transition = '0.2s';
        this.style.backgroundColor = '';
        this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
      });
    }

    });


content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

backBtn.addEventListener('click', function() {
  window.history.back();
});

searchBtn.addEventListener('click', function() {
      var searchInput = document.getElementById("search");
      var searchText = searchInput.value;
      $.ajax({
             url: '/management/teaservlet/searchByStuName',
             method: 'POST',
             data: {
                  searchText: searchText
             },
             success: function(response) {
                console.log(response);
                var search = response.data;
                console.log(search);
                if(search.length == 0){
                    alert("没有符合要求的学生！");
                }
                else{
                tbody.innerHTML = "";
                currentIndex = 1;
                 search.forEach(function(item) {
                            var row = document.createElement("tr");
                            row.innerHTML = `
                              <td>${currentIndex}</td>
                              <td>${item.Stu_id}</td>
                              <td>${item.Stu_name}</td>
                              <td>${item.Cou_name}</td>
                              <td>${item.Stu_major}</td>
                              <td>${item.Stu_class}</td>
                              <td>${item.Score}</td>
                              <td><button id="jjw${currentIndex}">退课</button></td>
                            `;
                            tbody.appendChild(row);
                            currentIndex++;
                          });

                        for (let i=1; i < currentIndex; i++) {
                              var jjw = document.getElementById('jjw'+i);
                            //  console.log(jjw);
                              jjw.addEventListener('click', function() {
                                // 编写跳转到修改信息页面的操作
                                // window.location.href = "editstupwd.html";
                                  var confirmLogout = confirm("确定要为该同学退课吗？");
                                    if (confirmLogout) {
                                      // 用户点击了确认按钮
                                      // 编写退出登录的操作
                                      //没选过这个课
                                      $.ajax({
                                            url: '/management/teaservlet/dropCourse',
                                            method: 'POST',
                                            data: {
                                              count: i
                                            },
                                            success: function(response) {
                                              console.log(response);
                                              console.log(response.data);
                //                              alert("选课成功！"+i);
                                              alert("退课成功！");
                                            }
                                          });
                                    } else {
                                      // 用户点击了取消按钮
                                      // 可以选择执行其他操作或不进行任何操作
                                    }
                              });
                            }
             }
             },
             error: function(xhr, status, error) {
                console.log(error);
             }

          });
});